//
//  CycNewRefreshctrol.swift
//  微博cyc
//
//  Created by cyc on 16/3/8.
//  Copyright © 2016年 cyc. All rights reserved.
//

import UIKit

class CycNewRefreshctrol: UIRefreshControl {
    // 7-3-1箭头旋转的偏移值
    private let refreshControlOffest: CGFloat = -60
    
    /// 标示位
    private var isUp = false

    // 覆盖父类的属性
    // 添加override
    // 实现didSet
    override var frame: CGRect {
        didSet {
            print("frame: \(frame)")
            
            if frame.origin.y > 0 {
                return
            }
            
            // 7-3能到下面来y为负数
            // 0 - 60箭头向下
            // 小于-60,箭头向上
            if frame.origin.y <= refreshControlOffest && !isUp {
                isUp = true
                refreshView.rotationTipIcon(isUp)
                print("动画箭头向上")
            } else if frame.origin.y > refreshControlOffest && isUp {
                isUp = false
                refreshView.rotationTipIcon(isUp)
                   print("动画箭头向下")
            }
            //7-5-3
            if refreshing{
                refreshView.startloading()
            }
             //出现动画重复
            
            
        }
    }
//7-1构造frame
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init() {
        super.init()
        
        prepareUI()
    }
    
    
    //7-6写一个方法
    /**
    tableVie回到原来的位置结束刷新
    */
    override func endRefreshing()
    
    {
        
            super.endRefreshing()
           refreshView.stopLoading()
            
        
    }
    private func prepareUI(){
        // 添加子控件
        addSubview(refreshView)
        
        
        // 添加约束
        refreshView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(refreshView.frame.size)
            make.centerX.centerY.equalTo(self)
        }
        
        print("refreshView: \(refreshView.frame)")
        
    }
    // MARK: - 懒加载
    //7-1
    /// 刷新的自定义view,从xib里面加载出来的view,在xib里面设置有多大加载出来就有多大
    private lazy var refreshView: CycNewRefreshView = CycNewRefreshView.refreshView()

}
//箭头旋转做好了7-1-7-4 旋转方向
//动画旋转了7-5
